package com.op.teachingliteapp.sender.ui.view.device;

import android.os.AsyncTask;
import android.widget.ArrayAdapter;


/**
 * Created by Genji on 2015/5/16.
 */
public class DeviceScan extends AsyncTask<ArrayAdapter, DeviceDisplay, String> {
    private ArrayAdapter adapter;
    private DeviceDisplay deviceDisplay;

    public DeviceScan(ArrayAdapter adapter, DeviceDisplay deviceDisplay) {
        this.adapter = adapter;

        this.deviceDisplay = deviceDisplay;
    }


    @Override
    protected void onProgressUpdate(DeviceDisplay... values) {
        deviceAdd(values[0]);


    }

    @Override
    protected String doInBackground(ArrayAdapter... params) {
        publishProgress(deviceDisplay);


        return null;
    }

    private void deviceAdd(DeviceDisplay deviceDisplay) {
        int position = adapter.getPosition(deviceDisplay);
        if (position >= 0) {
            adapter.remove(deviceDisplay);
            adapter.insert(deviceDisplay, position);
        } else {
            adapter.insert(deviceDisplay,0);
            adapter.notifyDataSetChanged();
        }


    }
}
